﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
 
using System.Text;
using System.Windows.Forms;

namespace Demo
{
    public partial class PDFExport : DemoBaseUserControl
    {
        public PDFExport()
        {
            InitializeComponent();
            this.Title = "打印到 PDF";
            this.Description = "Spread .NET  提供将 SpreadSheet 导出为 PDF 格式功能，并且可以自定义导出内容和样式设置。本例中单击“打印设置”按钮进行自定义设定，单击“导出到 PDF”按钮即可打印到 PDF 格式文件。";
        }

        private void PDFExport_Load(object sender, EventArgs e)
        {
            FpSpread1.DataSource = 销售明细表TableAdapter.GetData();
            FpSpread1.ScrollBarTrackPolicy = FarPoint.Win.Spread.ScrollBarTrackPolicy.Both;

            FpSpread1.Sheets[0].Columns[0].Visible = false;
            FpSpread1.Sheets[0].Columns[1].AllowAutoSort = true;
            FpSpread1.Sheets[0].Columns[7].AllowAutoFilter = true;

            for (int intC = 0; intC < FpSpread1.Sheets[0].ColumnCount; intC++)
                FpSpread1.Sheets[0].Columns[intC].Width = 80F;
           
        }
        void FpSpread1_PrintMessageBox(object sender, FarPoint.Win.Spread.PrintMessageBoxEventArgs e)
        {
            if (e.BeginPrinting == false)
            {
                if (MessageBox.Show("要查看 PDF 文件吗？", "PDF 导出完成", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    System.Diagnostics.Process.Start(saveFileDialog1.FileName);

            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.FpSpread1.ShowPageSetup(this.FpSpread1_Sheet1, false, true);
            
        }

        private void butExport_Click_1(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "保存 Spread 为 PDF 格式";
            saveFileDialog1.Filter = "Pdf 文件 |*.pdf";

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FpSpread1.Sheets[0].PrintInfo.PrintToPdf = true;
                FpSpread1.Sheets[0].PrintInfo.PdfWriteMode = FarPoint.Win.Spread.PdfWriteMode.New;
                FpSpread1.Sheets[0].PrintInfo.PdfWriteTo = FarPoint.Win.Spread.PdfWriteTo.File;
                FpSpread1.Sheets[0].PrintInfo.PdfFileName = saveFileDialog1.FileName;
                FpSpread1.Sheets[0].PrintInfo.Preview = false;
                FpSpread1.Sheets[0].PrintInfo.AbortMessage = "打印结束";
                FpSpread1.PrintSheet(0);
            }
        }
    }
}
